package com.example.demo.listener;

import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.read.listener.ReadListener;
import com.example.demo.entity.excel.UserExcel;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class DemoDataListener implements ReadListener<UserExcel> {
    private final List<UserExcel> userList = new ArrayList<>();

    @Override
    public void invoke(UserExcel user, AnalysisContext context) {
        // 要保证数据类型相匹配，否则会抛出异常
        userList.add(user);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("所有数据读取完成！");

        for (UserExcel user : userList) {
            String text = String.format("姓名[%s]，别名[%s]，年龄%s岁", user.getName(), user.getOtherName(), user.getAge());
            log.info(text);
        }
    }
}
